package java_thinking.unit_21._21_7;

import java.util.concurrent.BlockingQueue;

/**
 * @Desc:   7-7 不可取消的任务在退出前恢复中断
 * @author: cww
 * @DateTime: 2019-06-13 23:06
 */

public class NoncancelableTask {
    public Task getNextTask(BlockingQueue<Task> queue) {
        boolean interrupted = false;
        try {
            while (true) {
                try {
                    return queue.take();
                } catch (InterruptedException e) {
                    interrupted = true;
                    //重新尝试
                }
            }
        } finally {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
interface Task {
}